using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SmartLink.Infrastructure
{
    /// <summary>
    /// GPS点纠偏
    /// </summary>
    public class GPSRectification
    {
        /// <summary>
        /// 百度GPS点纠偏
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="ak"></param>
        /// <param name="lat"></param>
        /// <param name="lng"></param>
        /// <returns></returns>
        public static T BaiDuRectification<T>(string ak, double lat, double lng)
        {
            var url = "http://api.map.baidu.com/geoconv/v1/?coords=";//后期优化这些地址都需要配置到config中
            var parmUrl = $"{lng},{lat}&from=1&to=5&ak={ak}";
            var result = HttpRequestHelper.Get(url + parmUrl);
            var gpsPoints = result.DeserializeObject<T>();
            return gpsPoints;
        }

        public static System.Drawing.PointF? BaiDuRectification(string ak, double lat, double lng)
        {
            System.Drawing.PointF? point = null;
            var url = "http://api.map.baidu.com/geoconv/v1/?coords=";//后期优化这些地址都需要配置到config中
            var parmUrl = $"{lng},{lat}&from=1&to=5&ak={ak}";
            var result = HttpRequestHelper.Get(url + parmUrl);
            var gpsPoints = result.DeserializeObject<BaiDuGPSPointDTO>();
            if (gpsPoints.result != null && gpsPoints.result.Any())
            {
                var tmp = new System.Drawing.PointF();
                tmp.Y = (float)gpsPoints.result[0].y;
                tmp.X = (float)gpsPoints.result[0].x;
                point = tmp;
            }
            return point;
        }


        private class BaiDuGPSPointDTO
        {
            /// <summary>
            /// 状态：正常0，异常非0，详细见状态码说明
            /// </summary>
            public int status { get; set; }

            /// <summary>
            /// 结果
            /// </summary>
            public IList<BaiDuPointDTO> result { get; set; }

            public class BaiDuPointDTO
            {
                /// <summary>
                /// lng
                /// </summary>
                public double x { get; set; }

                /// <summary>
                /// lat
                /// </summary>
                public double y { get; set; }
            }
        }
    }
}
